﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_unit_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindDDL();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindDDL()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlComList.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlComList.DataBind();

            this.ddlBuildList.DataSource = dal.GetBuild(int.Parse(this.ddlComList.SelectedValue), null, true);
            this.ddlBuildList.DataBind();
        }

        private void bindBuild()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlBuildList.DataSource = dal.GetBuild(int.Parse(this.ddlComList.SelectedValue), null, true);
            this.ddlBuildList.DataBind();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.Del_face_m_unit_base(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetUnit(int.Parse(this.hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    try
                    {
                        this.ddlComList.ClearSelection(); this.ddlBuildList.ClearSelection(); this.ddlUseFor.ClearSelection();
                        this.ddlComList.Items.FindByValue(dt.Rows[0]["self_community_code"].ToString()).Selected = true;
                        bindBuild();
                        this.ddlBuildList.Items.FindByValue(dt.Rows[0]["self_build_code"].ToString()).Selected = true;
                        this.ddlUseFor.Items.FindByValue(dt.Rows[0]["usefor"].ToString()).Selected = true;
                    }
                    catch
                    { }

                    this.txtLevel.Text = dt.Rows[0]["self_level"].ToString();
                    this.txtPlace.Text = dt.Rows[0]["self_place"].ToString();
                    this.txtName.Text = dt.Rows[0]["self_name"].ToString();
                    this.txtSimpleName.Text = dt.Rows[0]["self_simplename"].ToString();
                    this.txtAllarea.Text = dt.Rows[0]["all_area"].ToString();
                    this.txtInarea.Text = dt.Rows[0]["in_area"].ToString();
                    this.txtRealarea.Text = dt.Rows[0]["real_area"].ToString();
                    this.txtDirection.Text = dt.Rows[0]["direction"].ToString();
                    this.txtStruct.Text = dt.Rows[0]["struct"].ToString();
                    this.txtStyle.Text = dt.Rows[0]["unit_style"].ToString();
                    this.txtSealPrice.Text = dt.Rows[0]["saleprice"].ToString();
                    this.radStatusY.Checked = Convert.ToBoolean(dt.Rows[0]["mystatus"]);
                    this.radStatusN.Checked = !Convert.ToBoolean(dt.Rows[0]["mystatus"]);
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sCommunity = this.ddlComList.SelectedValue;
            string sBuild = this.ddlBuildList.SelectedValue;
            string sLevel = this.txtLevel.Text.Trim();
            string sPlace = this.txtPlace.Text.Trim();
            string sName = this.txtName.Text.Trim();
            string sSimpleName = this.txtSimpleName.Text.Trim();
            string sAllArea = this.txtAllarea.Text.Trim();
            string sInArea = this.txtInarea.Text.Trim();
            string sRealArea = this.txtRealarea.Text.Trim();
            string sDirect = this.txtDirection.Text.Trim();
            string sStruct = this.txtStruct.Text.Trim();
            string sStyle = this.txtStyle.Text.Trim();
            string sUseFor = this.ddlUseFor.SelectedValue;
            string sSalePrice = this.txtSealPrice.Text.Trim();
            bool bStatus = this.radStatusY.Checked;
            int iLevel = 0;
            if (sCommunity == string.Empty) { PM.Web.Common.JsAlert(this, "社区机构不能为空！"); return; }
            if (sBuild == string.Empty) { PM.Web.Common.JsAlert(this, "社区楼栋不能为空！"); return; }
            if (sName == string.Empty){PM.Web.Common.JsAlert(this, "单元名称不能为空！");return;}
            if (sSimpleName == string.Empty){PM.Web.Common.JsAlert(this, "单元简称不能为空！");return;}
            if (sLevel == string.Empty){PM.Web.Common.JsAlert(this, "楼层不能为空！"); return;
            }
            else
            {
                if (!int.TryParse(sLevel, out iLevel)) { PM.Web.Common.JsAlert(this, "楼层为数字！"); return; }
            }
            decimal dAllArea = 0; decimal dInArea = 0; decimal dRealArea = 0; decimal dSalePrice = 0;
            if (sAllArea != string.Empty) { if (!decimal.TryParse(sAllArea, out dAllArea)) { PM.Web.Common.JsAlert(this, "建筑面积为数字！"); return; } }
            if (sInArea != string.Empty) { if (!decimal.TryParse(sInArea, out dInArea)) { PM.Web.Common.JsAlert(this, "套内面积为数字！"); return; } }
            if (sRealArea != string.Empty) { if (!decimal.TryParse(sRealArea, out dRealArea)) { PM.Web.Common.JsAlert(this, "实用面积为数字！"); return; } }
            if (sSalePrice != string.Empty) { if (!decimal.TryParse(sSalePrice, out dSalePrice)) { PM.Web.Common.JsAlert(this, "参考市值为数字！"); return; } }
            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                if (dal.Add_face_m_unit_base(int.Parse(sCommunity), int.Parse(sBuild), iLevel, sPlace, sName, sSimpleName, 0, "", "", "", bStatus, dAllArea, dInArea, dRealArea, dSalePrice, sDirect, sStruct, sStyle, sUseFor, ""))
                {
                    PM.Web.Common.JsAlert(this, "保存成功！",true);
                }
            }
            else
            {
                if (dal.Modify_face_m_unit_base(int.Parse(this.hidId.Value), int.Parse(sCommunity), int.Parse(sBuild), iLevel, sPlace, sName, sSimpleName, 0, "", "", "", bStatus, dAllArea, dInArea, dRealArea, dSalePrice, sDirect, sStruct, sStyle, sUseFor, ""))
                {
                    PM.Web.Common.JsAlert(this, "修改成功！",true);
                }
            }
        }

        protected void ddlComList_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindBuild();
        }
    }
}